package com.vincce.sport.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.vincce.sport.core.entity.DateViewSession;
import com.vincce.sport.core.utils.RedisUtil;
import com.vincce.sport.entity.InvitationCode;
import com.vincce.sport.entity.Page;
import com.vincce.sport.mapper.InvitationCodeMapper;

@Controller
@RequestMapping("/invitationCode")
public class InvitationCodeController extends BaseController{
	@Resource
	private InvitationCodeMapper invitationCodeMapper;
	/**
	 * 分页查询
	 * @param model
	 * @param page
	 * @param city
	 * @return
	 */
	@RequestMapping("/invitationCodeList")
	public String invitationCodeList(Model model,Page<InvitationCode> page){
		Map<String, Object> filter = new HashMap<String, Object>();	
		filter.put("userId", getCurrentUser().getId());
		page.setFilter(filter);
		page.setTotal(invitationCodeMapper.queryByInvitationCodeCount(page.getFilter()));
		page.setResult(invitationCodeMapper.queryByInvitationCodePage(page));
		model.addAttribute("page",page);
		return "/backstage/invitationCode";
	}
	
	
	/**
	 * 新增
	 * @param model
	 * @param city
	 * @return
	 */
	@RequestMapping("/addInvitationCode")
	@ResponseBody
	public String addInvitationCode(Model model,int size){
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", getCurrentUser().getId());
		Long num = invitationCodeMapper.queryByInvitationCodeCount(map);
		if(num >= 20){
			result.put("suc", false);
			result.put("msg", "您的邀请码数量已达上限，不能再生产邀请码！");
	        return JSON.toJSONString(result);
		}
		//判断size长度
		if(num.intValue()+size>20){
			size=20-num.intValue();
		}
		InvitationCode invitationCode=new InvitationCode();
		Boolean flag=false;		
		invitationCode.setCreateTime(new Date());
		invitationCode.setStatus("否");
		invitationCode.setUserId(getCurrentUser().getId().intValue());
		
		for(int i=0;i<size;i++){				
			String code=getRCode();
			//添加到redis
			Set<String> set=RedisUtil.cacheGet(RedisUtil.DB_SPORT_CODE, "codeKey", Set.class);
			if(set == null){
				set = new HashSet<String>();
			}
			try{
				boolean bl=set.add(code);
				if(bl){
					RedisUtil.cacheAddUpdate(RedisUtil.DB_SPORT_CODE, "codeKey", set, -1);
					invitationCode.setCode(code);
					Long count=invitationCodeMapper.insertInvitationCode(invitationCode);
					flag=count>0?true:false;
				}				
			}catch(Exception e){
				e.printStackTrace();
			}			
		}		
		
		result.put("suc", flag);
        return JSON.toJSONString(result);
	}
	
	
	/**
	 * 修改
	 * @param model
	 * @param invitationCode
	 * @return
	 */
	@RequestMapping("/updateInvitationCode")
	@ResponseBody
	public String updateInvitationCode(Model model,InvitationCode invitationCode){
		Boolean flag=false;
		
		Long count=invitationCodeMapper.updateInvitationCode(invitationCode);
		flag=count>0?true:false;
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("suc", flag);
        return JSON.toJSONString(result);
	}
	
	/**
	 * 根据邀请码查询
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/selectByInvitationCode")
	@ResponseBody
	public String selectByInvitationCode(Model model,String code){
		InvitationCode invitationCode=invitationCodeMapper.selectByInvitationCode(code);
        return JSON.toJSONString(invitationCode);
	}
	
	public String getRCode(){
		String str="";
		String str1="";
		for(int j=0;j<1;j++){//你想生成几个字符的，就把3改成几，如果改成１,那就生成一个随机字母．
		 str= str+(char) (Math.random ()*26+'A');
		 str1= str1+(char) (Math.random ()*26+'a');
		}
		int random=(int)(Math.random()*9000+1000);
		return str+str1+random;
	}
}
